<?php
/*
*--------------------------------------------------------------
* 框架路由,负责解析URL并运行APP
*--------------------------------------------------------------
* 最后修改时间 2011-4-13 Leon
* @version 1.0
* @author Leon(tmkook@gmail.com)
* @date 2010-2-27
*--------------------------------------------------------------
*/
class route{

    //验证URI安全
    function __construct(){
        $allowed_uri_chars = '|'.C('route/allowed_uri_chars').'|';
        if(preg_match($allowed_uri_chars,$_SERVER['REQUEST_URI'])){
            ERROR_404(L('_PAGE_NOT_FOUND_'));
        }
    }

    //解析URI
	function parse(){
		if(isset($_GET['controller']) || isset($_GET['action'])) return true;
		$file = end(explode('/',$_SERVER['SCRIPT_NAME']));
		$request = parse_url($_SERVER['REQUEST_URI']);
		$request = $request['path'];
		if(strpos($request,$file)){
			$request = trim(str_replace($_SERVER['SCRIPT_NAME'],'',$request),'/');
		}else{
			$base = str_replace('/'.$file,'',$_SERVER['SCRIPT_NAME']);
			$request = str_replace('/'.$file,'',$request);
			$request = trim(str_replace($base,'',$request),'/');
		}
		if(empty($request)) return true;
		(array)$request = explode('/',$request);
		if(isset($request[0])){
			$_GET['controller'] = $request[0];
			unset($request[0]);
		}
		if(isset($request[1])){
			$_GET['action'] = $request[1];
			unset($request[1]);
		}
		if(count($request) > 1){
			$id = 0;
			foreach($request as $value){
				$id++;
				if($id % 2 == 0){
					$val[] = $value;
				}else{
					$key[] = $value;
				}
			}
			if(count($key) !== count($val)) $val[] = NULL;
			$get = array_combine($key,$val);
			foreach($get as $key=>$value){
				$_GET[$key] = $value;
			}
		}
		if(0 == get_magic_quotes_gpc()){
			foreach($_REQUEST as $key=>$value){
				$_REQUEST[$key] = addslashes($value);
			}
		}
		return true;
	}

    //运行APP
	function run(){
		if(empty($_GET['controller'])){
			$_GET['controller'] = C('route/default_controller');
		}
		if(empty($_GET['action'])){
			$_GET['action'] = 'index';
		}
		if(strpos($_GET['controller'],C('route/controller_dir_trigger'))){
			$controller_file = _APP_.'coding/controller/'.str_replace(C('route/controller_dir_trigger'),'/',$_GET['controller']).'Controller.php';
			$controller = end(explode(C('route/controller_dir_trigger'),$_GET['controller'])).'Controller';
		}else{
			$controller = $_GET['controller'].'Controller';
			$controller_file = _APP_.'coding/controller/'.$controller.'.php';
		}
		if(!file_exists($controller_file)){
			$message = C('ini/debug')? L('_CONTROLLER_EXISTS_').':'.$controller.'.php' : L('_PAGE_NOT_FOUND_');
			ERROR_404($message);
		}
		include_once $controller_file;
		if(!class_exists($controller)){
			$message = C('ini/debug')? L('_CONTROLLER_UNDEFINE_').':'.$controller : L('_PAGE_NOT_FOUND_');
			ERROR_404($message);
		}
		$c = new $controller ();
		if(!method_exists($c,$_GET['action'])){
			$message = C('ini/debug')? L('_ACTION_UNDEFINE_').':'.$_GET['action'] : L('_PAGE_NOT_FOUND_');
			ERROR_404($message);
		}
		try{
			$c-> $_GET['action'] ();
		}catch(exceptions $e){
			$e->trace();
		}
	}
}